package com.heima.wemedia.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.apis.article.IArticleClient;
import com.heima.apis.user.IUserClient;
import com.heima.model.comment.dto.CommentRepayDto;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.WeMediaCommentDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vo.WeMediaCommentVo;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.utils.thread.ApThreadLocalUtil;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WeMediaCommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.ExecutableUpdateOperation;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.Query;
import java.util.Date;
import java.util.List;


@SuppressWarnings("ALL")
@Service
@Slf4j
@Transactional
public class WeMediaCommentServiceImpl implements WeMediaCommentService {

    @Autowired
    private IArticleClient articleClient;
    @Autowired
    private WmUserMapper mapper;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IUserClient iUserClient;


    @Override
    public ResponseResult findCommentList(WeMediaCommentDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser wmUser = WmThreadLocalUtil.getWmUser();
        if (wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        dto.checkParam();
        //插询作者ID
        WmUser wmUser1 = mapper.selectById(wmUser.getId());
        if (wmUser1==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer apAuthorId = wmUser1.getApAuthorId();
        //将作者ID设置到dto
        dto.setWmAuthorId(apAuthorId);
        //远程调用article查询数据库
        PageResponseResult responseResult = articleClient.selectComment(dto);
        if (responseResult.getCode()==200) {

        }
        return responseResult;
    }

    /*
     * 评论回复
     * */
    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public ResponseResult commentRepay(CommentRepayDto dto) {

        if (StringUtils.isBlank(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验是否登录
        //从登录拦截器中获取到 userId
        WmUser wmUser = WmThreadLocalUtil.getWmUser();
        Integer wmUserId = wmUser.getId();
        //效验参数
        if (wmUserId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //评论大于140字
        if (dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_TOO_MORE);
        }
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setAuthorId(wmUserId.toString());
        //
        String authorName = wmUserMapper.selectById(wmUser.getId()).getName();
        apCommentRepay.setAuthorName(authorName);
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setUpdatedTime(new Date());
        // 将评论信息添加到数据库
        mongoTemplate.save(apCommentRepay);
        return ResponseResult.okResult("回复评论成功");
    }
}
